<template>
  <div class="home-container">
    <!-- 顶部导航栏 -->
    <HeaderNav />

    <!-- 主要内容区域 -->
    <div class="main-container">
      <!-- 轮播图/公告区域 -->
      <div class="banner">
        <div class="banner-content">
          <h1 class="banner-title">{{ bannerInfo.title }}</h1>
          <p class="banner-desc">{{ bannerInfo.description }}</p>
          <el-button class="banner-btn" @click="handleBannerClick">
            {{ bannerInfo.buttonText }}
          </el-button>
        </div>
        <div class="banner-image">
          <img v-if="bannerInfo.image" :src="bannerInfo.image" alt="Banner" />
          <div v-else class="banner-placeholder"></div>
        </div>
      </div>

      <!-- 分类导航 -->
      <section class="category-section">
        <h2 class="section-title">
          <div class="section-title-icon">分</div>
          商品分类
        </h2>

        <div class="category-list" v-loading="categoriesLoading">
          <CategoryCard
            v-for="category in categories"
            :key="category.id"
            :category="category"
          />
        </div>
      </section>

      <!-- 热门商品 -->
      <section class="product-section">
        <div class="product-header">
          <h2 class="section-title">
            <div class="section-title-icon">热</div>
            热门商品
          </h2>
          <el-link type="primary" @click="viewMore('hot')">
            查看全部 &gt;
          </el-link>
        </div>

        <div class="product-list" v-loading="hotProductsLoading">
          <ProductCard
            v-for="product in hotProducts"
            :key="product.id"
            :product="product"
          />
        </div>

        <el-empty
          v-if="!hotProductsLoading && hotProducts.length === 0"
          description="暂无热门商品"
        />
      </section>

      <!-- 新品上架 -->
      <section class="product-section">
        <div class="product-header">
          <h2 class="section-title">
            <div class="section-title-icon">新</div>
            新品上架
          </h2>
          <el-link type="primary" @click="viewMore('new')">
            查看全部 &gt;
          </el-link>
        </div>

        <div class="product-list" v-loading="newProductsLoading">
          <ProductCard
            v-for="product in newProducts"
            :key="product.id"
            :product="product"
          />
        </div>

        <el-empty
          v-if="!newProductsLoading && newProducts.length === 0"
          description="暂无新品商品"
        />
      </section>

      <!-- 系统公告 -->
      <section class="notice-section-wrapper">
        <NoticeList
          :notices="notices"
          title="系统公告"
          :show-more="true"
          :limit="5"
        />
      </section>
    </div>

    <!-- 底部信息 -->
    <footer class="footer">
      <div class="footer-content">
        <div class="footer-about">
          <div class="footer-logo">
            <div class="logo-icon">福</div>
            <div class="logo-text">企业福利商城</div>
          </div>
          <p class="footer-desc">
            企业员工专属福利平台，通过"卡+卡密"兑换机制，为员工提供安全便捷的福利兑换服务。
          </p>
        </div>

        <div class="footer-links">
          <div class="link-column">
            <div class="link-title">购物指南</div>
            <div class="link-item" @click="navigateTo('/help/shopping')">购物流程</div>
            <div class="link-item" @click="navigateTo('/help/member')">会员介绍</div>
            <div class="link-item" @click="navigateTo('/help/faq')">常见问题</div>
          </div>

          <div class="link-column">
            <div class="link-title">配送方式</div>
            <div class="link-item" @click="navigateTo('/help/pickup')">上门自提</div>
            <div class="link-item" @click="navigateTo('/help/delivery')">快递配送</div>
            <div class="link-item" @click="navigateTo('/help/shipping')">配送费用</div>
          </div>

          <div class="link-column">
            <div class="link-title">售后服务</div>
            <div class="link-item" @click="navigateTo('/help/refund')">退款说明</div>
            <div class="link-item" @click="navigateTo('/help/return')">返修/退换货</div>
            <div class="link-item" @click="navigateTo('/help/cancel')">取消订单</div>
          </div>

          <div class="link-column">
            <div class="link-title">关于我们</div>
            <div class="link-item" @click="navigateTo('/about/contact')">联系我们</div>
            <div class="link-item" @click="navigateTo('/about/privacy')">隐私协议</div>
            <div class="link-item" @click="navigateTo('/about/terms')">用户协议</div>
          </div>
        </div>
      </div>

      <div class="copyright">
        © 2023 企业福利商城 版权所有 | 客服热线：400-123-4567
      </div>
    </footer>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import HeaderNav from '@/components/HeaderNav.vue'
import ProductCard from '@/components/ProductCard.vue'
import CategoryCard from '@/components/CategoryCard.vue'
import NoticeList from '@/components/NoticeList.vue'
import { getCategoryList } from '@/api/category'
import { getHotProducts, getNewProducts } from '@/api/product'
import { getSystemNotices } from '@/api/notice'

const router = useRouter()

// 响应式数据
const categoriesLoading = ref(false)
const hotProductsLoading = ref(false)
const newProductsLoading = ref(false)

const categories = ref([])
const hotProducts = ref([])
const newProducts = ref([])
const notices = ref([])

// 轮播图信息
const bannerInfo = reactive({
  title: '员工专属福利季',
  description: '本月上新50+精选商品，使用福利卡兑换享额外优惠！年度积分即将清零，请及时使用。',
  buttonText: '立即兑换',
  image: null
})

// 获取商品分类
const fetchCategories = async () => {
  try {
    categoriesLoading.value = true
    const response = await getCategoryList()
    categories.value = response.data || []
    console.log('分类数据获取成功:', categories.value)
    if (categories.value.length > 0) {
      console.log('第一个分类示例:', categories.value[0])
    }
  } catch (error) {
    console.error('获取分类失败:', error)
    ElMessage.error('获取商品分类失败')
  } finally {
    categoriesLoading.value = false
  }
}

// 获取热门商品
const fetchHotProducts = async () => {
  try {
    hotProductsLoading.value = true
    const response = await getHotProducts({ limit: 8 })
    hotProducts.value = response.data || []
  } catch (error) {
    console.error('获取热门商品失败:', error)
    ElMessage.error('获取热门商品失败')
  } finally {
    hotProductsLoading.value = false
  }
}

// 获取新品商品
const fetchNewProducts = async () => {
  try {
    newProductsLoading.value = true
    const response = await getNewProducts({ limit: 8 })
    newProducts.value = response.data || []
  } catch (error) {
    console.error('获取新品商品失败:', error)
    ElMessage.error('获取新品商品失败')
  } finally {
    newProductsLoading.value = false
  }
}

// 获取系统公告
const fetchNotices = async () => {
  try {
    const response = await getSystemNotices()
    notices.value = response.data || []
  } catch (error) {
    console.error('获取公告失败:', error)
  }
}

// 处理轮播图按钮点击
const handleBannerClick = () => {
  router.push('/card-exchange')
}

// 查看更多商品
const viewMore = (type) => {
  router.push(`/products?type=${type}`)
}

// 页面导航
const navigateTo = (path) => {
  router.push(path)
}

// 组件挂载时获取数据
onMounted(() => {
  fetchCategories()
  fetchHotProducts()
  fetchNewProducts()
  fetchNotices()
})
</script>

<style lang="scss" scoped>
.home-container {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.main-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 20px;
}

/* 轮播图区域 */
.banner {
  height: 300px;
  background: linear-gradient(135deg, #409EFF 0%, #64b5ff 100%);
  border-radius: 12px;
  margin-bottom: 30px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 40px;
  color: white;
  position: relative;
  overflow: hidden;

  .banner-content {
    max-width: 50%;
    z-index: 2;
  }

  .banner-title {
    font-size: 32px;
    font-weight: 600;
    margin-bottom: 15px;
  }

  .banner-desc {
    font-size: 16px;
    line-height: 1.6;
    margin-bottom: 20px;
    opacity: 0.9;
  }

  .banner-btn {
    background-color: white;
    color: #409EFF;
    border: none;
    padding: 10px 24px;
    border-radius: 20px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    }
  }

  .banner-image {
    position: absolute;
    right: 0;
    bottom: 0;
    height: 100%;
    opacity: 0.8;
    z-index: 1;

    img {
      height: 100%;
      object-fit: cover;
    }

    .banner-placeholder {
      width: 300px;
      height: 100%;
      background: rgba(255, 255, 255, 0.2);
      border-radius: 10px 0 0 10px;
    }
  }
}

/* 分类导航 */
.category-section {
  margin-bottom: 30px;
}

.section-title {
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 20px;
  display: flex;
  align-items: center;

  .section-title-icon {
    width: 24px;
    height: 24px;
    background-color: #409EFF;
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 10px;
    color: white;
    font-size: 14px;
  }
}

.category-list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
}

/* 商品展示 */
.product-section {
  margin-bottom: 40px;
}

.product-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.product-list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

.notice-section-wrapper {
  margin-bottom: 40px;
}

/* 底部信息 */
.footer {
  background-color: white;
  padding: 30px 0;
  margin-top: 50px;
  border-top: 1px solid #e4e7ed;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
}

.footer-about {
  flex: 1;
}

.footer-logo {
  display: flex;
  align-items: center;
  margin-bottom: 15px;

  .logo-icon {
    width: 36px;
    height: 36px;
    background: linear-gradient(135deg, #409EFF 0%, #64b5ff 100%);
    border-radius: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 10px;
    color: white;
    font-weight: bold;
    font-size: 18px;
  }

  .logo-text {
    font-size: 20px;
    font-weight: 600;
    color: #409EFF;
  }
}

.footer-desc {
  color: #909399;
  max-width: 300px;
  line-height: 1.6;
}

.footer-links {
  display: flex;
}

.link-column {
  margin-right: 60px;

  &:last-child {
    margin-right: 0;
  }
}

.link-title {
  font-weight: 600;
  margin-bottom: 15px;
}

.link-item {
  margin-bottom: 10px;
  color: #606266;
  cursor: pointer;
  transition: color 0.3s;

  &:hover {
    color: #409EFF;
  }
}

.copyright {
  text-align: center;
  padding-top: 20px;
  margin-top: 20px;
  border-top: 1px solid #e4e7ed;
  color: #909399;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .product-list {
    grid-template-columns: repeat(3, 1fr);
  }

  .category-list {
    grid-template-columns: repeat(4, 1fr);
  }
}

@media (max-width: 768px) {
  .main-container {
    padding: 0 15px;
  }

  .banner {
    height: 200px;
    padding: 0 20px;

    .banner-title {
      font-size: 24px;
    }

    .banner-desc {
      font-size: 14px;
    }
  }

  .product-list {
    grid-template-columns: repeat(2, 1fr);
  }

  .category-list {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }

  .footer-content {
    flex-direction: column;
  }

  .footer-links {
    margin-top: 30px;
    flex-wrap: wrap;
  }

  .link-column {
    margin-right: 40px;
    margin-bottom: 20px;
  }
}

@media (max-width: 480px) {
  .banner {
    flex-direction: column;
    height: auto;
    padding: 20px;
    text-align: center;

    .banner-content {
      max-width: 100%;
      margin-bottom: 20px;
    }

    .banner-image {
      position: relative;
      height: 150px;

      .banner-placeholder {
        width: 200px;
        height: 100%;
        border-radius: 10px;
      }
    }
  }

  .product-list {
    grid-template-columns: 1fr;
  }

  .category-list {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .footer-links {
    justify-content: space-between;
  }

  .link-column {
    margin-right: 0;
    flex: 1;
    text-align: center;
  }
}
</style>